package com.shanghaionstar.nav.presenter;

import android.content.Context;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.shanghaionstar.nav.iview.INavAroundDealerView;
import com.onstar.cn.mag.dealer.AroundDealerRequestEntity;
import com.onstar.cn.mag.dealer.NavAroundDealerResponseEntity;
import com.shanghaionstar.nav.service.NavService;

import rx.functions.Action1;

/**
 * 周边经销商业务处理
 * Created by Yonglang Hong on 2016/7/21.
 */
public class NavAroundDealerPresenter extends BasePresenter{

    private NavService navService;

    private INavAroundDealerView aroundDealerView;

    public NavAroundDealerPresenter(IMVPScheduler mvpScheduler, INavAroundDealerView aroundDealerView, Context context) {
        super(mvpScheduler);
        this.aroundDealerView = aroundDealerView;
        navService = new NavService(context);
    }

    public void getAroundDealerList(final AroundDealerRequestEntity requestEntity){
        subscription = navService.getAroundDealerList(requestEntity)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<NavAroundDealerResponseEntity>() {
                    @Override
                    public void call(NavAroundDealerResponseEntity responseEntity) {
                        aroundDealerView.showMoreLayout(responseEntity.hasNextPage());
                        if (requestEntity.getPageIndex() <= 1
                                && (responseEntity.getList()== null || responseEntity.getList().isEmpty())){
                            aroundDealerView.showEmpty();
                        } else aroundDealerView.showDealers(responseEntity.getList());

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        if (aroundDealerView != null) {
                            aroundDealerView.onResponseError(new OnStarException(throwable));
                        }
                    }
                });
    }
}
